package com.qf.servlet;

import com.qf.service.DtsGoodsService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.GoodsCountVo;
import com.qf.vo.OrderCountVo;
import com.qf.vo.UserCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.servlet
 * @Description:
 * @Date 2022/10/18 16:40
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/admin/stat")
public class AdminStatController {

    @Autowired
    DtsUserService dtsUserService;

    @Autowired
    DtsOrderService dtsOrderService;

    @Autowired
    DtsGoodsService dtsGoodsService;

    /**
     * 用户统计
     */
    @RequestMapping("/user")
    public Object list(){
        //获取所有的用户
        List<UserCountVo> userList = dtsUserService.findUser();
        List<Map<String,Object>> rows =  new ArrayList<>();

        for (UserCountVo userCountVo : userList) {
            Map<String,Object> map = new HashMap<>();
            map.put("day",userCountVo.getDay());
            map.put("users",userCountVo.getUsers());
            rows.add(map);
        }
        String[] colums ={"day","users"};

        Map<String,Object> data = new HashMap<>();
        data.put("columns",colums);
        data.put("rows",rows);

        return ResponseUtil.ok(data);

    }

    /**
     * 订单统计
     */
    @RequestMapping("/order")
    public Object order(){
        //获取全部订单
        List<OrderCountVo> orders = dtsOrderService.findOrders();

        List<Map<String,Object>> rows = new ArrayList<>();

        for (OrderCountVo order : orders) {
            Map<String,Object> map = new HashMap<>();
            map.put("amount", order.getAmount());
            map.put("orders", order.getOrders());
            map.put("customers",order.getCustomers() );
            map.put("day",order.getDay() );
            map.put("pcr", order.getPcr());

            rows.add(map);
        }
        String [] columns = {"day", "orders", "customers", "amount", "pcr"};

        Map<String,Object> data = new HashMap<>();
        data.put("columns", columns);
        data.put("rows", rows);

        return ResponseUtil.ok(data);
    }

    /**
     * 商品统计
     */
    @RequestMapping("/goods")
    public Object goods(){
        //获取全部商品
        List<GoodsCountVo> count = dtsGoodsService.findCount();

        List<Map<String,Object>> rows = new ArrayList<>();
        for (GoodsCountVo goodsCountVo : count) {
            Map<String,Object> map = new HashMap<>();
            map.put("amount",goodsCountVo.getAmount());
            map.put("orders",goodsCountVo.getOrders());
            map.put("day",goodsCountVo.getDay());
            map.put("products",goodsCountVo.getProducts());
            rows.add(map);
        }

        String [] columns ={"day", "orders", "products", "amount"};

        Map<String,Object> data = new HashMap<>();
        data.put("columns", columns);
        data.put("rows",rows);

        return ResponseUtil.ok(data);


    }
}
